package com.itheima.web.servlet;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.itheima.domain.Province;
import com.itheima.service.ProvinceService;
import com.itheima.service.impl.ProvinceServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/provinceServlet")
public class ProvinceServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("womeifangwenma" );
      /* //调用service层
        ProvinceService service = new ProvinceServiceImpl();
        List<Province> list = service.findAll( );

        //我要使用Ajax技术,我传过去一个json形式的文本
        //把对象实例化为json格式
        ObjectMapper mapper =new ObjectMapper();
        String json = mapper.writeValueAsString(list);
        //把数据响应给浏览器页面
        //设置响应编码
        response.setContentType("application/json;charset=utf-8");
        response.getWriter().write(json);
        //看下json具体格式
        System.out.println(json );*/

      //使用 redis数据库来缓存
        //调用service层
        ProvinceService service = new ProvinceServiceImpl();
        String json = service.findRedis();
        //设置响应编码
        response.setContentType("application/json;charset=utf-8");
        response.getWriter().write(json);
        System.out.println(json );
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    this.doPost(request,response);
    }
}
